/*
 * Created by Huang 嵘才 on 18-2-7 上午11:22
 * Copyright (c) 2018. All rights reserved.
 * Last modified 18-2-5 上午11:07
 */

package io.esirong.image;

import android.app.Activity;
import android.content.Context;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

/**
 * ImageLoader 图片加载（参照面门设计模式）
 * Created by 黄嵘才(HuangRc) on 2018/2/2.
 */

public final class ImageLoader implements LoaderManager {

    private static volatile boolean isInitializing;

    /**
     * 初始化（在Application中调使用）
     * 在使用之前需要做初始工作，针对兼容的fresco在进行xml布局时是要求已调用init初始化的
     *
     * @param context Context
     */
    public static synchronized void init(Context context) {
        checkAndInitializeImageLoader(context);
    }

    private static void checkAndInitializeImageLoader(Context context) {
        // In the thread running initGlide(), one or more classes may call Glide.get(context).
        // Without this check, those calls could trigger infinite recursion.
        if (isInitializing) {
            throw new IllegalStateException("You cannot call Glide.get() in registerComponents(),"
                    + " use the provided Glide instance instead");
        }
        isInitializing = true;
        initializeImageLoader(context);
        isInitializing = false;
    }

    private static void initializeImageLoader(Context context) {
        Context applicationContext = context.getApplicationContext();
        ImageLoaderManger.init(applicationContext);
    }

    public static LoaderManager with(Context context) {
        return new ImageLoader();
    }

    public static LoaderManager with(Activity activity) {
        return new ImageLoader();
    }

    public static LoaderManager with(FragmentActivity activity) {
        return new ImageLoader();
    }

    public static LoaderManager with(android.app.Fragment fragment) {
        return new ImageLoader();
    }

    public static LoaderManager with(Fragment fragment) {
        return new ImageLoader();
    }

    public static LoaderManager with(View view) {
        return new ImageLoader();
    }

    @Override
    public void showImage(View v, String url) {
        showImage(v, url, LoaderOptions.OPTIONS_DEFAULT);
    }

    @Override
    public void showImage(View v, int drawable) {
        showImage(v, drawable, LoaderOptions.OPTIONS_DEFAULT_RES);
    }

    @Override
    public void showImage(View v, byte[] imageBytes) {
        showImage(v, imageBytes, LoaderOptions.OPTIONS_DEFAULT);
    }

    @Override
    public void showImage(View v, String url, ImageLoaderOptions options) {
        ImageLoaderStrategyManager.getInstance().showImage(v, url, options);
    }

    @Override
    public void showImage(View v, int drawable, ImageLoaderOptions options) {
        ImageLoaderStrategyManager.getInstance().showImage(v, drawable, options);
    }

    @Override
    public void showImage(View v, byte[] imageBytes, ImageLoaderOptions options) {
        ImageLoaderStrategyManager.getInstance().showImage(v, imageBytes, options);
    }
}
